package leetcode_401_500;

public class LeeCode_443 {
    public static void main(String[] args) {
        System.out.println(compress(new char[]{'a', 'a', 'b', 'b', 'c', 'c', 'c'}));
    }
    private static int compress(char[] chars) {
        int i = 1, n = chars.length, index = 0, count = 1;
        char now = chars[0];
        while (i < n){
            if (chars[i] != now){
                chars[index++] = now;
                if (count != 1){
                    String tmp = Integer.toString(count);
                    for (int j = 0; j < tmp.length(); j++) {
                        chars[index++] = tmp.charAt(j);
                    }
                    count = 1;
                }
                now = chars[i];
            }else {
                count++;
            }
            i++;
        }
        chars[index++] = now;
        if (count != 1){
            String tmp = Integer.toString(count);
            for (int j = 0; j < tmp.length(); j++) {
                chars[index++] = tmp.charAt(j);
            }
        }
        return index;
    }
}
